#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''

【需求详情】
根据excel，统计重点源采全率

'''

import sys

sys.path.append('/mnt/moonbrook/Moonbrook')
from aes import contact_aes, name_aes
import os
import re
import json
import time
import requests
import xlrd
import xlwt
import csv
from copy import deepcopy
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig, RobotConfig
from utils import Utils
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    week_min, week_max = Utils.get_date_range(ndays=7)

    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col
        self.init_file_v1()
        self.business_col = self.db.p_col('business')
        source_ls = [u'阿土伯',u'78挂靠',u'年报',u'58挂靠',u'58同城',u'赶集网',u'官网',u'顺企网',u'英才网联',u'八方资源',u'世界工厂',u'阿土伯v2',
                     u'hunt007',u'八方资源网']
        self.source_times_dict = {}
        # for source_name in source_ls:
        #     if source_name not in self.source_times_dict:
        #         self.source_times_dict[source_name] = {
        #             'all_count': 0,  # 抽样数
        #             'has_ent_md': 0,  # 有企业名单数
        #             'has_ent_contact': 0,  # 有企业联系方式数
        #         }

    def init_file_v1(self):
        xlsx_path = '/tmp/dsf/a.xlsx'
        workbook = xlrd.open_workbook(xlsx_path)
        self.sheet = workbook.sheet_by_index(0)  # 通过索引顺序获取
        head_ls = self.sheet.row_values(0)

        self.csvfile = open('/tmp/dsf/重点源.csv', 'w')
        self.writer = csv.writer(self.csvfile, delimiter=',', dialect='excel')
        self.writer.writerow(head_ls)

    def close_file_v1(self):
        self.csvfile.close()

    def run(self):
        '''
        1、处理excel每一行，实时写入csv文件
        2、非重点源跳过
        (来源，pageurls，是否采到，企业联系方式是否采到)


        '''
        for i in range(self.sheet.nrows - 1):
            one_row_info_ls = self.sheet.row_values(i + 1)
            if len(one_row_info_ls) == 15:
                source = one_row_info_ls[3].strip()
                if source not in self.source_times_dict:
                    self.source_times_dict[source] = {
                        'all_count': 0,  # 抽样数
                        'has_ent_md': 0,  # 有企业名单数
                        'has_ent_contact': 0,  # 有企业联系方式数
                    }

                    # print('{} 非重点源：{}'.format(i, source, ))
                    # self.writer.writerow(one_row_info_ls)
                    # continue

                self.source_times_dict[source]['all_count'] += 1
                contact_url = one_row_info_ls[4].strip()
                page_url = one_row_info_ls[5].strip()
                if contact_url.endswith('/'):
                    contact_url_2 = contact_url[:-1]
                else:
                    contact_url_2 = contact_url + '/'
                if page_url.endswith('/'):
                    page_url_2 = page_url[:-1]
                else:
                    page_url_2 = page_url + '/'
                is_down = one_row_info_ls[6]
                is_down_contact = one_row_info_ls[7]
                # print(len(one_row_info_ls))
                # print(source, contact_url, page_url, is_down, is_down_contact)
                _id = Utils.page_url_to_business_id(contact_url)
                _id_2 = Utils.page_url_to_business_id(page_url)
                _id_3 = Utils.page_url_to_business_id(page_url_2)
                _id_4 = Utils.page_url_to_business_id(contact_url_2)
                _filter = {'_id': {'$in': [_id, _id_2,_id_3,_id_4]}}
                projection = ['b2bContact']
                record_dic = self.business_col.find_one(filter=_filter, projection=projection)
                if record_dic:
                    is_down = True
                    self.source_times_dict[source]['has_ent_md'] += 1
                    contact_ls = record_dic.get('b2bContact', [])
                    if contact_ls:
                        is_down_contact = True
                        self.source_times_dict[source]['has_ent_contact'] += 1
                one_row_info_ls[6] = is_down
                one_row_info_ls[7] = is_down_contact
                self.writer.writerow(one_row_info_ls)


            else:
                print('{} has error length:{}'.format(i, len(one_row_info_ls)))
                self.writer.writerow(one_row_info_ls)

        tj_title_ls = [u'反向抽样统计']
        tj_head_ls = [u'来源',u'抽样数',u'有企业名单数',u'有企业联系方式数',u'最新抽样采全率',u'原始抽样采全率']
        self.writer.writerow(tj_title_ls)
        self.writer.writerow(tj_head_ls)
        for source_name,dict_value in self.source_times_dict.items():
            one_line_ls = [source_name]
            all_count = dict_value.get('all_count')
            has_ent_md = dict_value.get('has_ent_md')
            has_ent_contact = dict_value.get('has_ent_contact')
            one_line_ls.append(all_count)
            one_line_ls.append(has_ent_md)
            one_line_ls.append(has_ent_contact)
            if all_count != 0:
                one_line_ls.append('{:,.2%}'.format(float(has_ent_contact)/all_count))
            self.writer.writerow(one_line_ls)
        self.close_file_v1()


def run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = '采全率'
    run(file_name)
    # scheduler_run(file_name)
